﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    public class CategoryService : ICategoryService
    {
        public long AddNew(CategoryDTO dto)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Category> bs = new BaseService<Category>(ctx);
                var en=bs.GetAll().SingleOrDefault(e => e.Name == dto.Name);
                if (en!=null)
                {
                    throw new ArgumentException("该商品类别已存在");
                }
                Category cate = new Category();
                cate.Name = dto.Name;
                ctx.Categories.Add(cate);
                ctx.SaveChanges();
                return cate.Id;
            }
        }

        public CategoryDTO[] GetAll()
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Category> bs = new BaseService<Category>(ctx);
                return bs.GetAll().ToList().Select(c => ToDTO(c)).ToArray();
            }
        }

        public CategoryDTO GetById(long categoryId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Category> bs = new BaseService<Category>(ctx);
                var data = bs.GetById(categoryId);
                if (data == null)
                {
                    throw new ArgumentException($"Id为{categoryId}的商品类型不存在");
                }
                return ToDTO(data);
            }
        }

        public void MakeDelete(long categoryId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Category> bs = new BaseService<Category>(ctx);
                bs.MakeDelete(categoryId);
            }
        }

        public void Updata(long categoryId, string name)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Category> bs = new BaseService<Category>(ctx);
                var data = bs.GetById(categoryId);
                if (data == null)
                {
                    throw new ArgumentException($"商品id为{categoryId}的商品不存在");
                }
                data.Name = name;
                ctx.SaveChanges();
            }
        }

        public CategoryDTO ToDTO(Category cate)
        {
            CategoryDTO dto = new CategoryDTO();
            dto.CreateDateTime = cate.CreateDateTime;
            dto.Id = cate.Id;
            dto.Name = cate.Name;
            return dto;
        }

        public CategoryDTO GetByName(string name)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Category> bs = new BaseService<Category>(ctx);
                var res = bs.GetAll().SingleOrDefault(e => e.Name == name);
                return res == null ? null : ToDTO(res);
            }
        }
    }
}
